package com.mycompany.project.client.duch;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.mycompany.project.client.chee.MapGUI;
import com.mycompany.project.client.chee.Module;
import com.mycompany.project.client.chee.Rover;

/**
 * This class will display the minimum resource solution GUI.
 * @author LeeNou
 *
 */
public class MiniRSGUI {
     /**
     * Vertical panel for hPanel.
     */
   private final transient VerticalPanel verticalPanel = new VerticalPanel();
    /**
     * Button to update map.
     */
    private final transient Button updateMapBtn = new Button("Update Map");
    /**
     * Grid containing the map.
     */
    private static Grid miniGrid = new Grid(MapGUI.ROW, MapGUI.COL);
    /**
     * Number for cellpadding.
     */
    private static final int CELLPADDING = 4;
    /**
     * Public constructor.
     */
    public MiniRSGUI() {
        verticalPanel.setSize("675px", "569px");
         verticalPanel.setHorizontalAlign(HorizontalAlignment.CENTER);
         ScrollPanel scrollPanel = new ScrollPanel();
         scrollPanel.setSize("675px", "340px");
         initMap();
         miniGrid.setBorderWidth(2);
         miniGrid.setCellPadding(CELLPADDING);
        miniGrid.setStyleName("bgMap");
         scrollPanel.add(miniGrid);
         verticalPanel.add(scrollPanel);
         verticalPanel.add(updateMapBtn);
         verticalPanel.add(RoverCalibrationGUI.getGUI());
         initUpdateBtn();
         Rover.initRover();
         Rover.randPickRowNCol();
    }
    /**
     * Get the mini grid.
     * @return miniGrid is the map.
     */
    public static Grid getMiniGrid() {
         return miniGrid;
    }
    /**
     * The GUI.
     * @return verticalPanel contains the GUI.
     */
    public final VerticalPanel getGUI() {
         return verticalPanel;
    }
    /**
     * Gives the button a click handler.
     */
    private void initUpdateBtn() {
         updateMapBtn.addClickHandler(new ClickHandler() {

               @Override
               public void onClick(final ClickEvent event) {
                    initMap();
                  Rover.placeOnMap();
                    Window.alert("Updated Complete!");
               }
         });
    }
    /**
     * Initialized minimum resource map.
     */
    private void initMap() {
         miniGrid.clear();
         for (int i = 0; i < MapGUI.ROW; i++) {
              miniGrid.setWidget(i, 0, new HTML("<center><font color=\"white\">"
                     + i + "</center></font>"));
         }
         for (int j = 0; j < MapGUI.COL; j++) {
             if (j < Module.MAX_SIZE) {
                 miniGrid.setWidget(0, j, new HTML(
                         "<font color=\"white\"><center>_" + j
                                 + "</center></font>"));
             } else {
                 miniGrid.setWidget(0, j, new HTML(
                         "<font color=\"white\"><center>" + j
                                 + "</center></font>"));
             }
         }
       /**  for (int i = 0; i < ModuleList.getMlist().size(); i++) {
        *     for (int j = 0; j < ModuleList.getMlist().get(i).size(); j++) {
        *          Image img = new Image(Module.getModuleImage(i));
        *          img.setTitle(ModuleList.getMlist()
        *          .get(i).get(j).getString());
        *          miniGrid.setWidget(
        *                 ModuleList.getMlist().get(i).get(j).getPositionY(),
        *                 ModuleList.getMlist().get(i).get(j).
        *                 getPositionX(),img);
        *     }
        * }
        */
    }
}
